﻿namespace Core
{
    public abstract class Component : IComponent
    {
        public long InstanceId { get; set; }

        public bool IsDisposed => InstanceId == 0;

        public string Name { get; set; }

        public SystemContext S { get; private set; }
        public AbstractGameContext G { get; private set; }
        public Entity Entity { get; private set; }

        // ReSharper disable once InconsistentNaming
        public virtual void SetSG(SystemContext s, AbstractGameContext g, Entity entity = null)
        {
            S = s;
            G = g;
            Entity = entity;
        }

        public virtual void Dispose()
        {
            if (IsDisposed) return;

            G.Factory.Remove(this);

            G = null;
            S = null;
            Entity = null;

            InstanceId = 0;

            ReferencePool.Recycle(this);
        }
    }
}
